<template>
  <div class="demo">
    <div class="login">
      <div class="title">登录</div>
      <el-form :rules="rules" :model="Login_Form" ref="ruleFormRef">

        <el-form-item prop="username">
          <el-input v-model="Login_Form.username" clearable maxlength="11" type="string" size="large"
            placeholder="请输入你的账号">
          </el-input>
        </el-form-item>


        <el-form-item prop="PassWord">
          <el-input v-model="Login_Form.PassWord" clearable show-password maxlength="12" type="string"
            class="Input_Username" size="large" placeholder="请输入你的密码">
          </el-input>
        </el-form-item>

        <div>
          <el-form-item prop="agreement">
            <el-radio-group v-model="Login_Form.agreement">
              <el-radio label="我已阅读，理解并同意"></el-radio>
            </el-radio-group>
            <span style="color:#409EFF">《隐私政策及条款和条件》</span>
          </el-form-item>

        </div>

      </el-form>
      <div class="button">
        <el-button @click="submitForm(ruleFormRef)" style="height: 50px; width: 100%;">立即登录</el-button>
      </div>
      <div class="Register" @click="Register">没有账号去注册</div>


    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { rules } from '../../utils/Method/Check.js' //引入校验规则
import { Logins } from '../../ApI/function/Login.js'
const Router = useRouter()


const Login_Form = reactive({
  username: null,  // 用户输入用户名
  PassWord: null,  // 用户输入密码
  agreement: null
})

/* 登录之表单校验 */
const ruleFormRef = ref()
const submitForm = (formEl) => {
  if (!formEl) return
  formEl.validate((valid, firlds) => {
    if (valid) {
      Loginsaxios()
    } else {
    }
  })
}
// 跳转至注册
const Register = () => {
  Router.push('/register')
}

const Loginsaxios = () => {
  Logins({
    u: Login_Form.username,
    p: Login_Form.PassWord,
  }).then(res => {
    console.log(res)
    if (res.code == "0000") {
      ElMessage({
        message: '恭喜你登录成功',
        type: 'success',
      })
      localStorage.setItem('token', res.data[0].token)
      localStorage.setItem('name', res.data[0].nick)
      localStorage.setItem('id', res.data[0].id)
      Router.push('/')

    } else {
      ElMessage.error(res.msg)
    }
  })

}

</script>

<style lang="less" scoped>
.demo {
  display: flex;
  height: 100vh;
  background-color: #F5F6F8;
  background-image: url(https://ycc888.oss-cn-hangzhou.aliyuncs.com/%E7%8C%BF%E6%9F%A5%E6%9F%A5%E5%9B%BE%E7%89%87/login.png);


  .login {
    width: 500px;
    height: 400px;
    border-radius: 20px;
    background-color: #fff;
    margin: auto;
    padding: 50px;
    box-sizing: border-box;
    cursor: pointer;


    .el-input {
      height: 45px;
      margin-bottom: 5px;
    }

    .title {
      margin-bottom: 20px;
      font-size: 26px;
    }

    .Register {
      color: #409EFF;
      font-size: 14px;
      margin-top: 10px;
    }
  }
}
</style>